package com.zyd.shiro.business.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import com.zyd.shiro.persistence.beans.TrainMeeting;
import com.zyd.shiro.persistence.beans.User;
import com.zyd.shiro.persistence.dao.MallTrainMeetingDao;
import com.zyd.shiro.persistence.dao.MallUserDao;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;



import com.zyd.shiro.persistence.dao.MallTrainUserDao;
import com.zyd.shiro.persistence.beans.TrainUser;

/**
 * 服务层
 * 
 * @author RookieWZW
 *
 */
@Service
public class MallTrainUserService {

	@Autowired
	private MallTrainUserDao trainUserDao;
	@Autowired
	private MallTrainMeetingDao trainMeetingDao;
	@Autowired
	private MallUserDao userDao;
	

	public List<TrainUser> findAll() {

		return trainUserDao.findAllM();
	}


	/**返回当前报名且缴纳费用的人数
	 *
	 */
	public int countAllUserNumByTrainId(Integer trainId)
	{
		return  trainUserDao.countAllByTrain_idEquals(trainId);
	}

	public int getTrainMaxNum(Integer trainId)
	{
		TrainMeeting trainMeeting = trainMeetingDao.findOne(trainId);
		return trainMeeting.getTrain_maxnum();
	}
	public String getTrainName(Integer trainId)
	{
		TrainMeeting trainMeeting = trainMeetingDao.findOne(trainId);
		return trainMeeting.getMeeting_title();
	}
	public String getUserName(Integer userId)
	{
		User user = userDao.findOne(userId);
		return user.getNickName();
	}

	/**
	 * 返回报名但未缴纳费用的人数
	 * @param trainId
	 * @return
	 */
	public int findNotPayNum(Integer trainId){
		return trainUserDao.countAllByTrain_idEqualsAndNoPay(trainId);
	}

	/**
	 * 按照培训ID查询
	 */
	public List<TrainUser> findAllByTrainId(Integer trainId,int type)
	{
		if(type==2)
			return trainUserDao.getAllByTrain_idEquals(trainId);
		else
			return trainUserDao.getAllByTrain_idEquals(trainId,type);
	}
	/**
	 * 按照用户ID查询该用户参加的所有培训
	 */
	public List<TrainUser> finAllByUserId(Integer userId)
	{
		return trainUserDao.getAllByUser_idEquals(userId);
	}

	/**
	 * test
	 * @return
	 */

	/**
	 * 根据培训ID删除所有信息
	 * @param trainId
	 */
	public void DeletByTrainId(Integer trainId)
	{
		List<TrainUser> trainUserList = trainUserDao.getAllByTrain_idEquals(trainId);
		trainUserDao.delete(trainUserList);
	}
	/**
	 *根据会议Id和用户Id删除
	 */
	public void DeleteByTrainIdAndUserId(Integer trainId,Integer userId)
	{
		List<TrainUser> trainUserList = trainUserDao.getAllByTrain_idEqualsandUAndUser_idEquals(trainId,userId);

		trainUserDao.delete(trainUserList);
	}
	/**
	 * 查看用户是否报名
	 */
	public boolean isJoinTrain(Integer trainId,Integer userId)
	{

		if(trainUserDao.isJoinTheTrain(trainId,userId)==1)
			return true;
		else
			return false;
	}

	/**
	 * 分页查询
	 * 
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<TrainUser> findPage(int page, int size) {
		PageRequest pageRequest = new PageRequest(page-1, size);
		return trainUserDao.findAll(pageRequest);
	}

	private Specification<TrainUser> where(Map searchMap) {
		
		return new Specification<TrainUser>() {
          
			@Override
			public Predicate toPredicate(Root<TrainUser> root,CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 
                if (searchMap.get("user_id")!=null && !"".equals(searchMap.get("user_id"))) {
                	predicateList.add(cb.like(root.get("user_id").as(String.class), "%"+(String)searchMap.get("user_id")+"%"));
                }

                return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
                
            }
        };		
		
	}

	public Page<TrainUser> findSearch(Map whereMap, int page, int size) {
		Specification<TrainUser> specification = where(whereMap);
		PageRequest pageRequest = new PageRequest(page-1, size);
		return trainUserDao.findAll(specification, pageRequest);
	}

	public TrainUser findOne(Integer id) {
		return trainUserDao.findOne(id);
	}

	public void add(TrainUser trainUser) {
		
		trainUserDao.save(trainUser);
	}
	
	public void update(TrainUser trainUser) {
		trainUserDao.save(trainUser);
	}

	public void delete(Integer id) {
		trainUserDao.delete(id);
	}

	public void deleteList(Integer[] ids) {
		for (Integer id : ids) {
			trainUserDao.delete(id);
		}
	}

}
